const app = getApp();
const server = require("../../server/server.js");
Page({
    data:{
        modes:[
            {id:1,name:"物品置换"},
            {id:2,name:"物品出售"},
            {id:3,name:"寻找物品"}
        ],
        sm:0,
        types:[],
        st: 0,
        showPrice: false,
        imgs:[],
        count: 4
    },
    onLoad() {
        this.loadTypes();
    },
    onShow() {
        if (app.userInfo && app.userInfo.id){
            return;
        }
        wx.showModal({
            title:'授权注册',
            content:'需要完成授权注册后，才可以使用完整功能',
            showCancel:false,
            consfirmText:'去注册',
            confirmColor:"#3cc61f",
            success: (result) => {
                if(result.confirm) {
                    wx.switchTab({
                        url:'/pages/user/user'
                    });
                }
            }
        })
    },
    loadTypes() {
        server.loadTypeList()
            .then((data)=>{
                // console.log(data);
                this.setData({
                    types:data
                })
            })
            .catch(server.errorFunction);
    },
    changeType(event) {
        this.setData({
            st:event.detail.value
        });
    },
    changeMode(event) {
        this.setData({
            showPrice: event.detail.value == 1,
            sm: event.detail.value
        });
    },
    chooseImgs() {
        wx.chooseImage({
            count: this.data.count,
            sizeType: ['compressed'],
            sourceType:['album','camera'],
            success: (res) => {
                console.log(res);
                this.setData({
                    imgs:this.data.imgs.concat(res.tempFilePaths)
                });
            }
        })
    },
    removeImg({currentTarget: {dataset: {i}}}){
        let temp = this.data.imgs;
        temp.splice(i, 1);
        this.setData({
            imgs:temp
        });
    },
    formSubmit({detail:{value}}){
        wx.showLoading({
            title:"物品发布中......",
            mask:true,
        });
        // console.log(JSON.stringify(value));
        let params = value;
        if(!params.price) {
            params.price = 0;
        }
        params.mode = this.data.modes[params.mode].id;
        params.typeId = this.data.types[params.typeId].id;
        params.status = 1;
        params.userId = app.userInfo.id;
        // console.log(params);
        // 数据添加 
        // 1、选择的图片上传
        let tempImgs = this.data.imgs;
        let parr = [];
        for (let i = 0; i < tempImgs.length; i++ ){
            let tempPromise = server.uploadFile(tempImgs[i]);
            parr.push(tempPromise);
        }
        // 2、将图片的结果编译成 JSON 字符串 作为参数添加
        // 和其他参数一起进行产品添加
        Promise.all(parr)
            .then((dataArr)=>{
                let imgs = {};
                for (let i = 0; i < dataArr.length; i++ ) {
                    imgs["file" + (i + 1)] = dataArr[i].result;
                }
                params.imgurls = JSON.stringify(imgs);
                return server.addProduct(params);
            })
            .then((data)=>{
                wx.hideLoading();
                if(data.message == "success"){
                    wx.showToast({
                        title: '发布成功',
                        icon: 'success',
                        duration: 1500,
                        success: ()=>{
                            wx.switchTab({
                                url: '/pages/fing/find',
                            })     
                        },
                    });
                }
            })
            .catch(server.errorFunction);
    }
})